<template>
  <div class="app-container">
    <div class="content-container" v-if="!constructionMaterialBuyFormVisible">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="姓名">
            <el-input v-model="queryParams.ownName" placeholder="请输入姓名" clearable />
          </el-form-item>
          <!-- <el-form-item prop="keywords" label="联系方式">
            <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handleQuery="changePage"
          >
            <template #type="{ row, name }">
              {{ getLabelByValue(CONTACT_TYPE, row.type) }}
            </template>
            <template #constructionMaterialBuyStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[handleStatus(row.house)] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, handleStatus(row.house)) }}
              </el-tag>
            </template>
            <template #operation="{ row, name }">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleConstructionMaterialBuy(row)"
              >
                材料选购
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <construction-material-buy-form
      v-if="constructionMaterialBuyFormVisible"
      @back-to="constructionMaterialBuyFormVisible = false"
      ref="constructionMaterialBuyFormRef"
      @load-data="handleQuery"
      :status="buyStatus"
    ></construction-material-buy-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import HouseAPI, { HousePageQuery, HousePageVO } from "@/api/house/index";
import { getLabelByValue } from "@/utils/enums";
import { CONTACT_TYPE, UserTypeEnum } from "../../../enums/system/UserEnum";
import { BIND_STATUS_ENUM } from "@/enums/contact/ContactEnum";
import { HOUSE_STATUS, HOUSE_STATUS_MAP } from "@/enums/house/house-const";
import ConstructionSchemeForm from "@/views/house/houseConstruction/components/construction-scheme-form.vue";
import ConstructionLinkForm from "@/views/house/houseConstruction/components/construction-link-form.vue";
import ConstructionMaterialInstallForm from "@/views/house/houseConstruction/components/construction-material-install-form.vue";
import ConstructionMaterialBuyForm from "@/views/house/houseConstructionMaterialBuy/components/construction-material-buy-form.vue";

const loading = ref(false);
const constructionMaterialBuyFormRef = ref();
const constructionMaterialBuyFormVisible = ref(false);

const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<HousePageQuery>({
  ownName: "",
  mobile: "",
  stepInfoList: ["STEP_CONSTRUCTION_MATERIAL_BUY"],
  status: BIND_STATUS_ENUM.SUCCESS.value,
  typeList: [CONTACT_TYPE.MATERIAL_SUPPLIER.value],
  userType: UserTypeEnum.HOME_BUILDING_MATERIALS.value,
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<HousePageVO[]>([]);
const buyStatus = ref(null);
const tableColumnsData = ref([
  {
    prop: "house.ownName",
    label: "户主名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.region",
    label: "小区地址",
    showOverflowTooltip: true,
  },
  {
    prop: "house.neighborhood",
    label: "小区名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.houseNumber",
    label: "门牌号",
    showOverflowTooltip: true,
    width: 150,
  },
  {
    prop: "house.size",
    label: "面积（平方米）",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "type",
    label: "通讯录类型",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "type",
  },
  {
    label: "材料选购状态",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "constructionMaterialBuyStatus",
  },
  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  HouseAPI.getHouseContactPage(queryParams)
    .then((data) => {
      tableData.value = data.list || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

function changePage(val: any) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.ownName = "";
  queryParams.mobile = "";
  handleQuery();
}
async function handleConstructionMaterialBuy(val) {
  let cannotEdit = val.house.constructionMaterialBuyStatus === HOUSE_STATUS.APPROVED.value;
  constructionMaterialBuyFormVisible.value = true;
  await nextTick();
  constructionMaterialBuyFormRef.value.showModal(val, cannotEdit);
  buyStatus.value = handleStatus(val?.house);
}

function handleStatus(house: any) {
  // 如果是已经同意就直接返回已同意，如果不是需要判断
  if (house.constructionMaterialBuyStatus === HOUSE_STATUS.APPROVED.value) {
    return house.constructionMaterialBuyStatus;
  }
  // 如果内部有值，就说明是已经填写
  if (!!house?.houseConstructionMaterialBuyVO && house?.houseConstructionMaterialBuyVO?.length) {
    return HOUSE_STATUS.FILLED.value;
  }
  return house.constructionMaterialBuyStatus;
}
onMounted(async () => {
  handleQuery();
});
</script>
